#include "file.h"
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QDir>


file::file(QObject *parent)
    : QObject{parent}
{

}

file::~file()
{

}

void file::setFileName(QString fileName)
{
    if(fileName == this->m_fileName) {
        return ;
    }
    this->m_fileName = fileName;
}

QString file::getFileName()
{
    return this->m_fileName;
}

void file::setFilePath(QString filePath)
{
    if(filePath == this->m_filePath) {
        return ;
    }
    this->m_filePath = filePath;
    emit filePathChanged(this->m_filePath);
}

QString file::getFilePath()
{
    return this->m_filePath;
}

void file::setFileData(QByteArray fileData)
{
    this->m_fileData = fileData;
}

QByteArray file::getFileData()
{
    return this->m_fileData;
}

void file::clearFileData()
{
    this->m_fileData.clear();
}

void file::setFileList(QStringList fileList)
{
    this->m_fileList = fileList;
}

QStringList file::getFileList()
{
    return this->m_fileList;
}

void file::clearFileList()
{
    this->m_fileList.clear();
}
// 打开文件路径
bool file::openFileDirectory()
{
    QString filePath = QFileDialog::getExistingDirectory(NULL, tr("选择CSV文件路径"), "");
    if(filePath.isEmpty())
    {
        qDebug() << "not open file";
        return false;
    }
    this->m_filePath = filePath;

    QDir dir(filePath);
    QStringList nameFilters;
    nameFilters << "*";
    this->m_fileList = dir.entryList(nameFilters, QDir::Dirs|QDir::Readable, QDir::Name);
    this->m_fileList.removeOne(".");
    this->m_fileList.removeOne("..");

//    qDebug() << getFilePath();
//    qDebug() << getFileList();

    return true;
}
// 打开图片文件名
bool file::openImageFile()
{
    QString filePath = QFileDialog::getOpenFileName(NULL,tr("选择任意一张图片"),"",tr("Image(*.png *.jpg *.bmp)"));
    if(filePath.isEmpty())
    {
        qDebug() << "not open file";
        return false;
    }
    filePath.remove(filePath.split("/").constLast());
    setFilePath(filePath);

    QDir dir(filePath);
    QStringList nameFilters;
    nameFilters << "*.jpg" << "*.png" << "*.bmp";
    this->m_fileList = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);

//    qDebug() << getFilePath();
//    qDebug() << getFileList();

    return true;
}

bool file::openTxtFile()
{
    QString filePath = QFileDialog::getOpenFileName(NULL,tr("选择CSV文本文件"),"",tr("CSV(*.txt)"));
    if(filePath.isEmpty())
    {
        qDebug() << "not open file";
        return false;
    }
    setFileName(filePath.split("/").constLast());
    filePath.remove(filePath.split("/").constLast());
    setFilePath(filePath);

    return true;
}

bool file::openXmlFile()
{
    QString filePath = QFileDialog::getOpenFileName(NULL,tr("选择识别器文件"),"",tr("识别器文件(*.xml)"));
    if(filePath.isEmpty())
    {
        qDebug() << "not open file";
        return false;
    }
    setFileName(filePath.split("/").constLast());
    filePath.remove(filePath.split("/").constLast());
    setFilePath(filePath);

    return true;
}

bool file::saveFileImage(QImage img)
{
    QString filePath = QFileDialog::getSaveFileName(NULL,tr("保存图片"),"",tr("*.jpg"));
    if(filePath.isEmpty())
    {
        qDebug() << "not save file";
        return false;
    }
    setFilePath(filePath);

    QFile file(filePath);
    if(!file.open(QIODevice::WriteOnly))
    {
        QMessageBox::warning(NULL, "save file error", "<font size=\"4\">文件保存错误, 请重新保存</font>");
        return false;
    }
    img.save(&file, "JPEG");
    file.close();

    return true;
}

bool file::saveFileTxt(QString text)
{
    QString fileName = QFileDialog::getSaveFileName(NULL,tr("保存CSV文件"),"",tr("*.txt")); // 选择路径
    if(fileName.isEmpty())
    {
        qDebug() << "not save file";
        return false;
    }

    QFile file(fileName);
    if(!file.open(QIODevice::WriteOnly))
    {
       QMessageBox::warning(NULL, "save file error", "<font size=\"4\">文件保存错误, 请重新保存</font>");
       return false;
    }
    file.write(text.toUtf8());
    file.close();

    return true;
}
